package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.entity.MyCollectionBean;
import com.beiing.tianshuai.tianshuai.entity.UserInfoBean;
import com.beiing.tianshuai.tianshuai.util.TimeUtils;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.text.ParseException;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;

/**
 * Created by Jinni on 2017/7/17 0017.
 */

public class MyCollectionAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<MyCollectionBean.DataBean.NumberBean> mList;
    private Context mContext;
    private OnItemClickListener mListener;
    private String mUid;

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public MyCollectionAdapter(Context mContext, List<MyCollectionBean.DataBean.NumberBean> mList, String uid) {
        this.mContext = mContext;
        this.mList = mList;
        this.mUid = uid;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(mContext, R.layout.item_my_collection, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        final MyCollectionBean.DataBean.NumberBean bean = mList.get(position);
        RequestOptions requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .override(350, 350);
        Glide.with(mContext.getApplicationContext())
                .load(TextUtils.isEmpty(bean.getPhoto()) ? R.mipmap.default_avatar : HttpRequestConstant.USER_PIC_HEAD + bean.getPhoto())
                .apply(requestOptions)
                .into(((ViewHolder) holder).mUserAvatar);
        ((ViewHolder) holder).mUserName.setText(bean.getUname());
        ((ViewHolder) holder).mContactName.setText(bean.getContact());
        RequestOptions aPicRequestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .override(500, 280);
        Glide.with(mContext.getApplicationContext())
                .load(TextUtils.isEmpty(bean.getPhoto()) ? R.mipmap.default_avatar : HttpRequestConstant.ACTIVITY_PIC_HEAD + bean.getCover())
                .apply(aPicRequestOptions)
                .into(((ViewHolder) holder).mActivityPic);
        ((ViewHolder) holder).mActivityTitle.setText(bean.getLessons());
        try {
            ((ViewHolder) holder).mActivityEndTime.setText(TimeUtils.longToString(Long.parseLong(bean.getExitTime()),"MM-dd HH:mm"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        ((ViewHolder) holder).mActivityAddress.setText(bean.getLocation());
        ((ViewHolder) holder).contentView.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                mListener.onItemClickListener(holder.getAdapterPosition(), bean);
            }
        });
        if (TextUtils.equals(mUid, UserInfoBean.getUid(mContext))) {
            ((ViewHolder) holder).mDelete.setOnClickListener(new BGAOnNoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View view) {
                    if (mListener != null) {
                        mListener.onDelItemListener(holder.getAdapterPosition(), bean);
                    }
                }
            });
        } else {
            ((ViewHolder) holder).mDelete.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        } else {
            return 0;
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder{
        @BindView(R.id.user_avatar)
        CircleImageView mUserAvatar;
        @BindView(R.id.user_name)
        TextView mUserName;
        @BindView(R.id.contact_name)
        TextView mContactName;
        @BindView(R.id.activity_pic)
        ImageView mActivityPic;
        @BindView(R.id.activity_title)
        TextView mActivityTitle;
        @BindView(R.id.activity_end_time)
        TextView mActivityEndTime;
        @BindView(R.id.btn_delete)
        TextView mDelete;
        @BindView(R.id.activity_address)
        TextView mActivityAddress;

        View contentView;

        ViewHolder(View view) {
            super(view);
            contentView = view;
            ButterKnife.bind(this, view);
        }
    }

    public interface OnItemClickListener {
        void onItemClickListener(int position, MyCollectionBean.DataBean.NumberBean bean);
        void onDelItemListener(int position, MyCollectionBean.DataBean.NumberBean bean);
    }
}
